from datetime import datetime
from time import mktime
from wsgiref.handlers import format_date_time
import hmac
import hashlib
import base64
from urllib.parse import urlencode


def wmy():
    cur_time = datetime.now()
    date = format_date_time(mktime(cur_time.timetuple()))
    #print(date)
    tmp = "host: " + "spark-api.xf-yun.com" + "\n"
    tmp += "date: " + date + "\n"
    tmp += "GET " + "/v3.5/chat" + " HTTP/1.1"
    #print(tmp)

    tmp_sha = hmac.new("YzEyMjA3MjUxYTZmYWZmNmM2Y2RhMzg4".encode('utf-8'),  						digestmod=hashlib.sha256).digest()
    #print(tmp_sha)


    signature = base64.b64encode(tmp_sha).decode(encoding='utf-8')

    #print(signature)
    APIKey="tA2Mii3PIbev5sd8GGnzWObe/hQId62n8YFjDJgGWCc"
    authorization_origin = f"api_key='{APIKey}', algorithm='hmac-sha256', headers='host date request-line', signature='{signature}'"
    #print(authorization_origin)
    authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
    #print(authorization)

    v = {
            "authorization": authorization, # 上方鉴权生成的authorization
            "date": date,  # 步骤1生成的date
            "host": "spark-api.xf-yun.com" # 请求的主机名，根据具体接口替换
    }
    url = "wss://spark-api.xf-yun.com/v1.1/chat?" + urlencode(v)
    return url

   # print(url)






